package com.jiepos.mpos.core.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jiepos.mpos.biz.affix.service.AppAffixInfService;
import com.jiepos.mpos.common.constant.ExceptionConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Affix_InfEntity;
import com.jiepos.mpos.core.entity.MposResponse;
import com.jiepos.mpos.core.entity.UploadEntity;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.FileUtils;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * @ClassName: UploadController
 * @Description: 公共文件上传控制层
 * @author 孙永
 * @date 2017年9月30日 下午4:19:38
 */
@Controller
@RequestMapping("upload")
public class UploadController extends BaseController{
	
	@Autowired
	private AppAffixInfService appAffixInfService;

	/**
	 * 文件上传
	 * @param request
	 * @param file
	 * @param upload
	 * @return
	 */
	@RequestMapping(value="/image.do", method=RequestMethod.POST)
	@ResponseBody
	public MposResponse uploadImg(HttpServletRequest request, HttpServletResponse response, MultipartFile file, UploadEntity upload){
		MposResponse resp = new MposResponse();
		try{
			/*判断是否有重命名，无则系统自定义图片名称*/
			if(StringUtils.isBlank(upload.getUploadName())){
				upload.setUploadName(getNewFileName(file.getOriginalFilename()));
			}
			String filePath = null;
			/*修改，则上传路径沿用之前路径*/
			if(!StringUtils.isBlank(upload.getAffixIdx())){
				App_Affix_InfEntity entity = getAffixEntity(upload);
				if(entity != null && !StringUtils.isBlank(entity.getAffix_path())){
					filePath = entity.getAffix_path().substring(0, entity.getAffix_path().lastIndexOf("/"));
				}
			}
			/*根据业务类型生成图片保存的路径*/
			if(StringUtils.isBlank(filePath)){
				filePath = getFilePath(upload.getUploadType());
			}
			String uploadPath = request.getServletContext().getRealPath("/") + filePath;
			/*保存文件*/
			File _file = FileUtils.uploadFile(file, uploadPath, upload.getUploadName());
			if(_file != null){
				upload.setContentPath(request.getContextPath());
				upload.setAffixPath(filePath + "/" + upload.getUploadName());
				resp.setRespData(upload);
			}
		}catch(Exception e){
			e.printStackTrace();
			resp = new MposResponse(ExceptionConstant.ERROR);
		}
		return resp;
	}
	
	@RequestMapping(value="/showimage.do")
	@ResponseBody
	public MposResponse uploadImg(HttpServletRequest request, UploadEntity upload){
		MposResponse resp = new MposResponse();
		try{
			App_Affix_InfEntity entity = getAffixEntity(upload);
			if(entity != null){
				resp.setRespData(request.getContextPath() + entity.getAffix_path());
			}
		}catch(Exception e){
			e.printStackTrace();
			resp = new MposResponse(ExceptionConstant.ERROR);
		}
		return resp;
	}
	
	private App_Affix_InfEntity getAffixEntity(UploadEntity upload){
		App_Affix_InfEntity entity = appAffixInfService.getAffixInfByParam(upload.getAffixIdx(), upload.getAffixType(), upload.getStoreType());
		return entity;
	}
	
	/**
	 * 获取上传路径
	 * @param type
	 * @return
	 */
	private String getFilePath(String uploadType){
		StringBuffer path = new StringBuffer("/");
		path.append(SystemConstant.FILE_UPLOAD_PATH);
		if(!StringUtils.isBlank(uploadType)){
			path.append("/")
			.append(uploadType);
		}
		path.append("/")
		.append(DateUtil.getCurrDateStr());
		return path.toString();
	}
	
	/**
	 * 系统重命名文件
	 * @param fileName 原始名称
	 * @return
	 */
	private String getNewFileName(String fileName){
		fileName = fileName.substring(fileName.lastIndexOf("."));
		return IDFactoryUtil.getUUID() + fileName;
	}

}
